﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;



namespace sammerpracticsGame
{
    public partial class PixelCars : Form
    {
	static string Record;
	nen nj;t ljk;ty ,snm wbrk;
        private int MarkX, MarkY, XcordMyCar, Speed, SpeedShow, score, max, CountRecord;
        static int j, k, per, flagperestroenie;
        static string NamePlayer = "";
        static string Record;
        static char symbol;
        static int MainFlag, flagSpeed2, flagSpeed3, flagSpeed4, flagGameOver, flagWriteName;
        static int[] ModelCar = { 0, 1, 2, 3, 4, 5 };
        static int[] PositionCar = {151, 172, 199, 223, 250, 272};
        static int[] Cary2 = { -60, -120, -180, - 240, -300, -360, -420, -480, -540, -600, -660, -720, -780, -840, -900 };
        static int[] FlagCar = new int[10];
        static int[] Carx = new int[10];
        static int[] Cary = new int[10];
        static int[] ColorCar = new int[10];
        static int[] SpeedCar = { 0, 1, -1, 2, -2};
        static int[] SpeedCar2 = new int[10];
        static string[] RecordName = new string[20];
        static int[] RecordScore = new int[20];
        //меню
        PointF tochkaNewGame = new PointF(140, 150);
        PointF tochkaRecords = new PointF(140, 200);
        PointF tochkaHelp = new PointF(140, 250);
        PointF tochkaExit = new PointF(140, 300);
        PointF tochkaName = new PointF(140, 60);
        PointF tochkaBack4 = new PointF(200, 430);
        //игра
        PointF tochkaRecord = new PointF(500, -7);
        PointF tochkaRecord2 = new PointF(500, 30);
        PointF tochkaSpeed3 = new PointF(500, 120);
        PointF tochkaSpeed = new PointF(500, 150);
        PointF tochkaSpeed2 = new PointF(560, 150);
        PointF tochkaScore = new PointF(500, 90);
        PointF tochkaScore2 = new PointF(500, 60);
        PointF tochkaPayse = new PointF(500, 250);
        //помощь
        PointF tochkaHelp2 = new PointF(160, 10);
        PointF tochkaHelp3 = new PointF(50, 50);
        PointF tochkaBack = new PointF(160, 430);
        //шрифт
        Font sh = new Font("Comic Sans MS", 30);
        Font sp = new Font("Comic Sans MS", 25);
        Font frontstart = new Font("Comic Sans MS", 20);
        Font frontRestars = new Font("Comic Sans MS", 14);
        Point point;
        //таблица рекордов
        PointF tochkaRecords2 = new PointF(140, 10);
        //конец игры
        PointF tochkaGameOver = new PointF(140, 130);
        PointF tochkaGameOver2 = new PointF(90, 180);
        PointF tochkaSaveRecords = new PointF(100, 200);
        PointF tochkaYes = new PointF(150, 250);
        PointF tochkaNo = new PointF(280, 250);
        PointF tochkaSave = new PointF(200, 250);
        PointF tochkaEnterName = new PointF(170, 150);
        PointF[] tochkaScore3 = { new PointF(100, 70), new PointF(100, 100), new PointF(100, 130), new PointF(100, 160), new PointF(100, 190), new PointF(100, 220), new PointF(100, 250), new PointF(100, 280), new PointF(100, 310), new PointF(100, 340), new PointF(100, 370), new PointF(100, 400), new PointF(100, 430), new PointF(100, 460), new PointF(100, 490), new PointF(100, 520) };
        public PixelCars()
        {
            InitializeComponent();
        }
        private void PixelCars_Load(object sender, EventArgs e)
        {
            flagperestroenie = 0;
            LoadRecords();
            Random rand = new Random(DateTime.Now.Second);
            for (int i = 0; i < 6; i++)
            {
                FlagCar[i] = rand.Next(0, 2);
                if (FlagCar[i] == 1)
                {
                    SpeedCar2[i] = SpeedCar[rand.Next(0, 5)];
                    ColorCar[i] = ModelCar[rand.Next(0, 6)];
                    Carx[i] = PositionCar[rand.Next(0, 6)];
                    Cary[i] = Cary2[rand.Next(0, 15)];
                }      
            }       
            XcordMyCar = 213;//
            score = 0;
            SpeedShow = 0;
            MainFlag = 0;
            flagSpeed2 = 0;
            flagSpeed3 = 0;
            flagSpeed4 = 0;
            MarkX = -40;
            MarkY = 0;
            Speed = 0;
            flagWriteName = 0;
        }
        private void MyMarking(Graphics g, int N, int x1, int y1) // отрисовка разметки дороги
        {
            if (N == 0) return;
            else
            {   
                g.DrawLine(new Pen(Brushes.White, 5), 205, x1, 205, y1);
                g.DrawLine(new Pen(Brushes.White, 5), 255, x1, 255, y1);
                x1 += 70; 
                y1 += 70; 
                N--;
                MyMarking(g, N, x1, y1);
            }
        }
        private void MyMenu(Graphics g) //отрисовка меню
        {
            this.MinimumSize = new System.Drawing.Size(500, 500);
            this.MaximumSize = new System.Drawing.Size(500, 500);
            g.Clear(Color.Black);
            g.DrawString("Новая игра", sh, new SolidBrush(Color.White), tochkaNewGame);
            g.DrawString("Рекорды", sh, new SolidBrush(Color.White), tochkaRecords);
            g.DrawString("Помощь", sh, new SolidBrush(Color.White), tochkaHelp);
            g.DrawString("Выход", sh, new SolidBrush(Color.White), tochkaExit);
            g.DrawString("Pixel Cars", sh, new SolidBrush(Color.White), tochkaName);
            g.DrawString("2013", frontRestars, new SolidBrush(Color.White), tochkaBack4);
        }
        private void OtherCars(Graphics g, int Carx, int Cary, int c) // остальные машинки
        {
            Color c2 = Color.White;
            if (c == 1)
            {
                c2 = (Color.MediumBlue);
            }
            if (c == 0)
            {
                c2 = (Color.DeepPink);
            }
            if (c == 2)
            {
                c2 = (Color.Gold);
            }
            if (c == 3)
            {
                c2 = (Color.Lime);
            }
            if (c == 4)
            {
                c2 = (Color.SaddleBrown);
            }
            if (c == 5)
            {
                c2 = (Color.Snow);
            }      
            SolidBrush G = new SolidBrush(c2);
            SolidBrush B = new SolidBrush(Color.Black);
            //корпус
            Point[] tt = { new Point(10+Carx, 10+Cary), new Point(11+Carx, 10+Cary), new Point(11+Carx, 8+Cary), new Point(12+Carx, 8+Cary),
                             new Point(12+Carx, 7+Cary), new Point(13+Carx, 7+Cary), new Point(13+Carx, 6+Cary), new Point(14+Carx, 6+Cary), new Point(14+Carx, 5+Cary),
                             new Point(23+Carx, 5+Cary), new Point(23+Carx, 6+Cary), new Point(24+Carx, 6+Cary), new Point(24+Carx, 7+Cary), new Point(25+Carx, 7+Cary),
                             new Point(25+Carx, 8+Cary), new Point(26+Carx, 8+Cary), new Point(26+Carx, 10+Cary), new Point(27+Carx, 10+Cary), new Point(27+Carx, 18+Cary),
                             new Point(28+Carx, 18+Cary), new Point(28+Carx, 19+Cary), new Point(26+Carx, 19+Cary), new Point(26+Carx, 24+Cary), new Point(27+Carx, 24+Cary),
                             new Point(27+Carx, 35+Cary), new Point(26+Carx, 35+Cary), new Point(26+Carx, 36+Cary), new Point(25+Carx, 36+Cary), new Point(25+Carx, 37+Cary),
                             new Point(22+Carx, 37+Cary), new Point(22+Carx, 36+Cary), new Point(15+Carx, 36+Cary), new Point(15+Carx, 37+Cary), new Point(12+Carx, 37+Cary),
                             new Point(12+Carx, 36+Cary), new Point(11+Carx, 36+Cary), new Point(11+Carx, 35+Cary), new Point(10+Carx, 35+Cary), new Point(10+Carx, 24+Cary),
                             new Point(11+Carx, 24+Cary), new Point(11+Carx, 19+Cary), new Point(9+Carx, 19+Cary), new Point(9+Carx, 18+Cary), new Point(10+Carx, 18+Cary)};
            g.FillPolygon(G, tt);
            //стекло
            Point[] pp = { new Point(12+Carx, 19+Cary), new Point(12+Carx, 17+Cary), new Point(13+Carx, 17+Cary),
                             new Point(13+Carx, 16+Cary), new Point(14+Carx, 16+Cary), new Point(14+Carx, 15+Cary),
                             new Point(23+Carx, 15+Cary), new Point(23+Carx, 16+Cary), new Point(24+Carx, 16+Cary),
                             new Point(24+Carx, 17+Cary), new Point(25+Carx, 17+Cary), new Point(25+Carx, 19+Cary),
                             new Point(24+Carx, 19+Cary), new Point(24+Carx, 20+Cary), new Point(23+Carx, 20+Cary),
                             new Point(23+Carx, 21+Cary), new Point(14+Carx, 21+Cary), new Point(14+Carx, 20+Cary),
                             new Point(13+Carx, 20+Cary), new Point(13+Carx, 19+Cary) };
            g.FillPolygon(B, pp);
            //боковое окно левое
            Point[] kk = { new Point(13+Carx, 21+Cary), new Point(13+Carx, 23+Cary), new Point(14+Carx, 23+Cary),
                             new Point(14+Carx, 26+Cary), new Point(12+Carx, 26+Cary), new Point(12+Carx, 21+Cary), };
            g.FillPolygon(B, kk);
            //боковое окно правое
            Point[] kk2 = { new Point(24+Carx, 21+Cary), new Point(24+Carx, 23+Cary), new Point(23+Carx, 23+Cary),
                             new Point(23+Carx, 26+Cary), new Point(25+Carx, 26+Cary), new Point(25+Carx, 21+Cary), };
            g.FillPolygon(B, kk2);
            //двигатель
            Point[] dd = { new Point(16+Carx, 34+Cary), new Point(16+Carx, 32+Cary), new Point(17+Carx, 32+Cary),
                             new Point(17+Carx, 30+Cary), new Point(20+Carx, 30+Cary), new Point(20+Carx, 32+Cary),
                             new Point(21+Carx, 32+Cary), new Point(21+Carx, 34+Cary), };
            g.FillPolygon(B, dd);
        }
        private void MyCar(Graphics g, int x2, int y) // моя машинка
        {
            SolidBrush G = new SolidBrush(Color.DarkRed);
            SolidBrush B = new SolidBrush(Color.Black);
            //корпус
            Point[] tt = { new Point(10+x2, 10+y), new Point(11+x2, 10+y), new Point(11+x2, 8+y), new Point(12+x2, 8+y),
                             new Point(12+x2, 7+y), new Point(13+x2, 7+y), new Point(13+x2, 6+y), new Point(14+x2, 6+y), new Point(14+x2, 5+y),
                             new Point(23+x2, 5+y), new Point(23+x2, 6+y), new Point(24+x2, 6+y), new Point(24+x2, 7+y), new Point(25+x2, 7+y),
                             new Point(25+x2, 8+y), new Point(26+x2, 8+y), new Point(26+x2, 10+y), new Point(27+x2, 10+y), new Point(27+x2, 18+y),
                             new Point(28+x2, 18+y), new Point(28+x2, 19+y), new Point(26+x2, 19+y), new Point(26+x2, 24+y), new Point(27+x2, 24+y),
                             new Point(27+x2, 35+y), new Point(26+x2, 35+y), new Point(26+x2, 36+y), new Point(25+x2, 36+y), new Point(25+x2, 37+y),
                             new Point(22+x2, 37+y), new Point(22+x2, 36+y), new Point(15+x2, 36+y), new Point(15+x2, 37+y), new Point(12+x2, 37+y),
                             new Point(12+x2, 36+y), new Point(11+x2, 36+y), new Point(11+x2, 35+y), new Point(10+x2, 35+y), new Point(10+x2, 24+y),
                             new Point(11+x2, 24+y), new Point(11+x2, 19+y), new Point(9+x2, 19+y), new Point(9+x2, 18+y), new Point(10+x2, 18+y)};
            g.FillPolygon(G, tt);
            g.DrawLine(new Pen(Brushes.Gray, 1), 17 + x2, 5 + y, 17 + x2, 36 + y);
            g.DrawLine(new Pen(Brushes.Gray, 1), 20 + x2, 5 + y, 20 + x2, 36 + y);
            //стекло
            Point[] pp = { new Point(12+x2, 19+y), new Point(12+x2, 17+y), new Point(13+x2, 17+y),
                             new Point(13+x2, 16+y), new Point(14+x2, 16+y), new Point(14+x2, 15+y),
                             new Point(23+x2, 15+y), new Point(23+x2, 16+y), new Point(24+x2, 16+y),
                             new Point(24+x2, 17+y), new Point(25+x2, 17+y), new Point(25+x2, 19+y),
                             new Point(24+x2, 19+y), new Point(24+x2, 20+y), new Point(23+x2, 20+y),
                             new Point(23+x2, 21+y), new Point(14+x2, 21+y), new Point(14+x2, 20+y),
                             new Point(13+x2, 20+y), new Point(13+x2, 19+y) };
            g.FillPolygon(B, pp);
            //боковое окно левое
            Point[] kk = { new Point(13+x2, 21+y), new Point(13+x2, 23+y), new Point(14+x2, 23+y),
                             new Point(14+x2, 26+y), new Point(12+x2, 26+y), new Point(12+x2, 21+y), };
            g.FillPolygon(B, kk);
            //боковое окно правое
            Point[] kk2 = { new Point(24+x2, 21+y), new Point(24+x2, 23+y), new Point(23+x2, 23+y),
                             new Point(23+x2, 26+y), new Point(25+x2, 26+y), new Point(25+x2, 21+y), };
            g.FillPolygon(B, kk2);
            //двигатель
            Point[] dd = { new Point(16+x2, 34+y), new Point(16+x2, 32+y), new Point(17+x2, 32+y),
                             new Point(17+x2, 30+y), new Point(20+x2, 30+y), new Point(20+x2, 32+y),
                             new Point(21+x2, 32+y), new Point(21+x2, 34+y), };
            g.FillPolygon(B, dd);
        }
        private void MyRecord(Graphics g) //таблица рекордов
        {
            LoadRecords();
            this.MinimumSize = new System.Drawing.Size(500, 500);
            this.MaximumSize = new System.Drawing.Size(500, 500);
            g.Clear(Color.Black);
            g.DrawString("Рекорды", sh, new SolidBrush(Color.White), tochkaRecords2);
            g.DrawString("В меню", frontstart, new SolidBrush(Color.White), tochkaBack);
        }
        private void GameOver(Graphics g) //экран конца игры
        {
            this.MinimumSize = new System.Drawing.Size(500, 500);
            this.MaximumSize = new System.Drawing.Size(500, 500);
            g.Clear(Color.Black);
            g.DrawString("Хотите сохранить счет?", frontstart, new SolidBrush(Color.White), tochkaSaveRecords);
            g.DrawString("Да", frontstart, new SolidBrush(Color.White), tochkaYes);
            g.DrawString("Нет", frontstart, new SolidBrush(Color.White), tochkaNo);
        }
        private void SaveName(Graphics g) // запись имени игрока
        {
            this.MinimumSize = new System.Drawing.Size(500, 500);
            this.MaximumSize = new System.Drawing.Size(500, 500);
            g.Clear(Color.Black);
            SolidBrush textBoxBrush = new SolidBrush(Color.White);
            RectangleF drawRect = new RectangleF(155, 195, 200, 40);
            SolidBrush borderBrush = new SolidBrush(Color.Black);
            StringFormat drawFormat = new StringFormat();
            drawFormat.Alignment = StringAlignment.Center;
            g.FillRectangle(textBoxBrush, 155, 200, 200, 30);
            g.DrawString(NamePlayer, frontstart, borderBrush, drawRect, drawFormat);
            g.DrawString("Введите имя:", frontstart, new SolidBrush(Color.White), tochkaEnterName);
            g.DrawString("Сохранить", frontstart, new SolidBrush(Color.White), tochkaSave); 
        }
        private void PixelCars_paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (MainFlag == 0)
            {
                MyMenu(g);
            }
            if (MainFlag == 1)
            {
                this.MinimumSize = new System.Drawing.Size(678, 712);
                this.MaximumSize = new System.Drawing.Size(678, 712);
                g.Clear(Color.Gray);
                g.DrawLine(new Pen(Brushes.Goldenrod, 10), 154, 0, 154, 800);
                g.DrawLine(new Pen(Brushes.Goldenrod, 10), 306, 0, 306, 800);
                SolidBrush B = new SolidBrush(Color.Green);
                g.FillRectangle(B, 0, 0, 149, 800);
                g.FillRectangle(B, 311, 0, 160, 800);
                g.FillRectangle(new SolidBrush(Color.Peru), 471, 0, 200, 800);
                g.DrawString("Рекорд:", sp, new SolidBrush(Color.White), tochkaRecord);
                ShowRecords(g);
                g.DrawString(Convert.ToString(SpeedShow), sp, new SolidBrush(Color.White), tochkaSpeed);
                g.DrawString(Convert.ToString(score), sp, new SolidBrush(Color.White), tochkaScore);
                g.DrawString("км/ч", sp, new SolidBrush(Color.White), tochkaSpeed2);
                g.DrawString("Скорость:", sp, new SolidBrush(Color.White), tochkaSpeed3);
                MyMarking(g, 11, MarkX, MarkY);
                g.DrawString("Пауза", sp, new SolidBrush(Color.White), tochkaPayse);
                for (int i = 0; i < 10; i++)
                {
                    if (FlagCar[i] != 0)
                    {
                        OtherCars(g, Carx[i], Cary[i], ColorCar[i]);
                    }
                    for (int j = 0; j < 10; j++)
                    {
                         if (i != j)
                         {
                             if (Cary[i] < -20)
                             {
                                 if (((Cary[i] >= Cary[j]) && (Cary[i] <= Cary[j] + 37)) || ((Cary[i] <= Cary[j]) && (Cary[i] >= Cary[j] + 37)))
                                 {
                                     Random rand = new Random(DateTime.Now.Millisecond);
                                     Carx[i] = PositionCar[rand.Next(0, 6)];
                                     Cary[i] = Cary2[rand.Next(0, 15)];
                                 }
                             }
                             if (FlagCar[j] == 1 && FlagCar[i] == 1)
                             {
                                 if (Carx[i] == Carx[j])
                                 {
                                     if ((Cary[j] - 20 >= Cary[i] + 52)||(Cary[j] + 52 <= Cary[i] - 20))
                                     {
                                         SpeedCar2[j] = SpeedCar2[i];
                                     }
                                 }
                             }
                             if((Cary[i] > 0)&&(Cary[j]>0))
                             {
                                 if (((Carx[j] + 5 >= Carx[i] + 2) && (Carx[j] + 5 <= Carx[i] + 17) && (Cary[j] >= Cary[i]) && (Cary[j] <= Cary[i] + 32)) || ((Carx[j] + 1 >= Carx[i] + 2) && (Carx[j] + 1 <= Carx[i] + 17) && (Cary[j] >= Cary[i]) && (Cary[j] <= Cary[i] + 32) || (Carx[j] + 14 >= Carx[i] + 2) && (Carx[j] + 14 <= Carx[i] + 17) && (Cary[j] >= Cary[i]) && (Cary[j] <= Cary[i] + 32) || (Carx[j] + 18 >= Carx[i] + 2) && (Carx[j] + 18 <= Carx[i] + 17) && (Cary[j] >= Cary[i]) && (Cary[j] <= Cary[i] + 32)) || ((Carx[j] + 1 >= Carx[i] + 1) && (Carx[j] + 1 <= Carx[i] + 15) && (Cary[j] + 32 >= Cary[i]) && (Cary[j] + 32 <= Cary[i] + 32)) || ((Carx[j] + 18 >= Carx[i] + 4) && (Carx[j] + 18 <= Carx[i] + 18) && (Cary[j] + 32 <= Cary[i] + 32) && (Cary[j] + 32 >= Cary[i])))
                                 {
                                     PointF tochkaCrash = new PointF(Carx[j]-30, Cary[j]);
                                     PointF tochkaCrash2 = new PointF(Carx[i] - 30, Cary[i]);
                                     SpeedCar2[j] = -8;
                                     SpeedCar2[i] = -8;
                                     g.DrawString("Авария", sp, new SolidBrush(Color.Red), tochkaCrash);
                                     g.DrawString("Авария", sp, new SolidBrush(Color.Red), tochkaCrash2);
                                     FlagCar[i] = 2;
                                     FlagCar[j] = 2;
                                 }
                             }
                         }
                    }
                }
                MyCar(g, XcordMyCar, 575);
                g.DrawString("Очки:", sp, new SolidBrush(Color.White), tochkaScore2);
                if (Speed > 0)
                {
                    timer5.Enabled = true;
                }
                for (int i = 0; i < 10; i++)
                {    
                    if (((XcordMyCar + 5 >= Carx[i] + 2) && (XcordMyCar + 5 <= Carx[i] + 17) && (575 >= Cary[i]) && (575 <= Cary[i] + 32)) || ((XcordMyCar + 1 >= Carx[i] + 2) && (XcordMyCar + 1 <= Carx[i] + 17) && (580 >= Cary[i]) && (580 <= Cary[i] + 32) || (XcordMyCar + 14 >= Carx[i] + 2) && (XcordMyCar + 14 <= Carx[i] + 17) && (575 >= Cary[i]) && (575 <= Cary[i] + 32) || (XcordMyCar + 18 >= Carx[i] + 2) && (XcordMyCar + 18 <= Carx[i] + 17) && (580 >= Cary[i]) && (580 <= Cary[i] + 32)) || ((XcordMyCar + 1 >= Carx[i] + 1) && (XcordMyCar + 1 <= Carx[i] + 15) && (607 >= Cary[i]) && (607 <= Cary[i] + 32)) || ((XcordMyCar + 18 >= Carx[i] + 4) && (XcordMyCar + 18 <= Carx[i] + 18) && (607 <= Cary[i] + 32) && (607 >= Cary[i])))
                    {
                        g.DrawString("Конец игры", sh, new SolidBrush(Color.Black), tochkaGameOver);
                        g.DrawString("Щелкните мышкой для продолжения", frontRestars, new SolidBrush(Color.Black), tochkaGameOver2);
                        timer1.Enabled = false;
                        timer5.Enabled = false;
                        flagGameOver = 1;
                    }
                } 
            }
            if (MainFlag == 2)
            {
                g.Clear(Color.Black);
                this.MinimumSize = new System.Drawing.Size(500, 500);
                this.MaximumSize = new System.Drawing.Size(500, 500);
                g.DrawString("Помощь:", frontstart, new SolidBrush(Color.White), tochkaHelp2);
                g.DrawString("  Управление автомобилем осуществляеться\nс помощью стрелок на клавиатуре.\nСмещение автомобиля происходит с помощью\nстрелок вправо и влево.\nРазгон автомобиля автомотический,\nтребуеться только переключать передачи\nстрелкой вверх.\nТорможение стрелка вниз.\n  Игра идет до первой аварии.", frontRestars, new SolidBrush(Color.White), tochkaHelp3);
                g.DrawString("В меню", frontstart, new SolidBrush(Color.White), tochkaBack);
            }
            if (MainFlag == 3)
            {
                MyRecord(g);
                ShowRecords(g);
            }
            if (MainFlag == 4)
            {
                GameOver(g);
            }
            if (MainFlag == 5)
            {
                SaveName(g);
            }
        }
        private void PixelCars_KeyDown(object sender, KeyEventArgs e)
        {
            if (MainFlag == 1)
            {
                if (timer1.Enabled == true)
                {
                    if (e.KeyCode == Keys.Left)
                    {
                        if (Speed > 0)
                        {
                            if (XcordMyCar >= 150)
                            {
                                XcordMyCar = XcordMyCar - 5;
                            }
                        }
                    }
                    if (e.KeyCode == Keys.Right)
                    {
                        if (Speed > 0)
                        {
                            if (XcordMyCar <= 270)
                            {
                                XcordMyCar = XcordMyCar + 5;
                            }
                        }
                    }
                    if (e.KeyCode == Keys.Up)
                    {
                        if (Speed >= 8)
                        {
                            if (Speed >= 12)
                            {
                                if (Speed < 16)
                                {
                                    flagSpeed4 = 0;
                                    timer4.Enabled = true;
                                }
                            }
                            else
                            {
                                flagSpeed3 = 0;
                                timer3.Enabled = true;
                            }
                        }
                        else
                        {
                            flagSpeed2 = 0;
                            timer2.Enabled = true;
                        }
                    }
                    if (e.KeyCode == Keys.Down)
                    {
                        if (Speed <= 12)
                        {
                            if (Speed <= 8)
                            {
                                if (Speed != 0)
                                {
                                    flagSpeed2 = 1;
                                    timer2.Enabled = true;
                                }
                            }
                            else
                            {
                                flagSpeed3 = 1;
                                timer3.Enabled = true;
                            }
                        }
                        else
                        {
                            flagSpeed4 = 1;
                            timer4.Enabled = true;
                        }
                    }
                }
            }
        }
        private void PixelCars_MouseClick(object sender, MouseEventArgs e)
        {
            point.X = e.X;
            point.Y = e.Y;
            if (MainFlag == 0)
            {
                if ((point.X > 140) && (point.X < 350) && (point.Y > 150) && (point.Y < 200))//новая игра
                {
                    NamePlayer = "";
                    score = 0;
                    MainFlag = 1;
                    XcordMyCar = 213;
                    MarkX = -40;
                    flagSpeed2 = 0;
                    flagSpeed3 = 0;
                    flagSpeed4 = 0;
                    SpeedShow = 0;
                    MarkY = 0;
                    Speed = 0;
                    Random rand = new Random(DateTime.Now.Second);
                    for (int i = 0; i < 10; i++)
                    {
                        FlagCar[i] = rand.Next(0, 2);
                        if (FlagCar[i] == 1)
                        {
                            SpeedCar2[i] = SpeedCar[rand.Next(0, 5)];
                            ColorCar[i] = ModelCar[rand.Next(0, 6)];
                            Carx[i] = PositionCar[rand.Next(0, 6)];
                            Cary[i] = Cary2[rand.Next(0, 15)];
                        }
                    }
                }
                if ((point.X > 140) && (point.X < 269) && (point.Y > 310) && (point.Y < 345))//выход
                {
                    Close();
                }
                if ((point.X > 140) && (point.X < 305) && (point.Y > 255) && (point.Y < 300))//помощь
                {
                    MainFlag = 2;
                }
                if ((point.X > 140) && (point.X < 310) && (point.Y > 210) && (point.Y < 250))//рекорды
                {
                    MainFlag = 3;  
                }
            }
            if ((MainFlag == 2) || (MainFlag == 3))
            {
                if ((point.X > 160) && (point.X < 260) && (point.Y > 433) && (point.Y < 470))//назад
                {
                    MainFlag = 0;
                    Refresh();
                }
            }
            if (MainFlag == 1)
            {
                if ((point.X > 500) && (point.X < 600) && (point.Y > 257) && (point.Y < 287))//пауза
                {
                    if (timer1.Enabled == true)
                    {
                        timer1.Enabled = false;
                        timer5.Enabled = false;
                    }
                    else
                    {
                        timer1.Enabled = true;
                        timer5.Enabled = true;
                    }
                }
            }
            if (MainFlag == 4)
            {
                if ((point.X > 280) && (point.X < 340) && (point.Y > 250) && (point.Y < 280))//Нет
                {
                    MainFlag = 0;
                    timer1.Enabled = true;
                }
                if ((point.X > 150) && (point.X < 190) && (point.Y > 250) && (point.Y < 280))//Да
                {
                    MainFlag = 5;
                    timer1.Enabled = true;
                }
            }
            if (flagGameOver == 1)
            {
                if ((point.X > 0) && (point.X < 678) && (point.Y > 0) && (point.Y < 720))//продолжение
                {
                    MainFlag = 4;
                    flagGameOver = 2;
                    Refresh();
                }
            }
            if (MainFlag == 5)
            {
                if ((point.X > 200) && (point.X < 320) && (point.Y > 250) && (point.Y < 280))//сохранить
                {
                    MainFlag = 3;
                    WriteRecords();
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)// основной таймер
        {
            MarkX += Speed;
            MarkY += Speed;
            if (MarkX >= 30)
            {
                MarkX = -40;
                MarkY = 0;
            }
            Random rand = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < 10; i++)
            {
                if (FlagCar[i] == 0)
                {
                    FlagCar[i] = 1;
                    SpeedCar2[i] = SpeedCar[rand.Next(0, 5)];
                    ColorCar[i] = ModelCar[rand.Next(0, 6)];
                    Carx[i] = PositionCar[rand.Next(0, 6)];
                    Cary[i] = Cary2[rand.Next(0, 15)];
                }
            }
            if (flagperestroenie != 0)
            {
                Perestroenie();
            }
            Refresh();
        }
        private void timer2_Tick(object sender, EventArgs e)// первая скорость
        {
            if (flagSpeed2 == 0)
            {
                Speed += 2;
                if (Speed >= 8)
                {
                    Speed = 8;
                    timer2.Enabled = false;
                }
            }
            if (flagSpeed2 == 1)
            {
                Speed -= 2;
                if (Speed <= 0)
                {
                    SpeedShow = 0;
                    Speed = 0;
                    timer2.Enabled = false; 
                }
            }
        }
        private void timer3_Tick(object sender, EventArgs e)// вторая скорость
        {
            if (flagSpeed3 == 0)
            {
                Speed += 1;
                if (Speed >= 12)
                {
                    Speed = 12;
                    timer3.Enabled = false;
                }
            }
            if (flagSpeed3 == 1)
            {
                Speed -= 1;
                if (Speed <= 8)
                {
                    Speed = 8;
                    timer3.Enabled = false;
                }
            }
        }
        private void timer4_Tick(object sender, EventArgs e)//третия скорость
        {
            if (flagSpeed4 == 0)
            {
                Speed += 1;
                if (Speed >= 16)
                {
                    timer4.Enabled = false;
                }
            }
            if (flagSpeed4 == 1)
            {
                Speed -= 1;
                if (Speed <= 12)
                {
                    Speed = 12;
                    timer4.Enabled = false;
                }
            }
            if (Speed >= 16)
            {
                Speed = 16;
            }
        }
        private void timer5_Tick(object sender, EventArgs e) // начисление очков
        {
            if((score != 0)&&(Speed==0))
            {
                for (int i = 0; i < 10; i++)
                {
                    if (FlagCar[i] != 2)
                    {
                        Cary[i] -= 6;
                    }
                }
            }
            if (Speed > 0)
            {
                for (int i = 0; i < 10; i++)
                {
                    Cary[i] += Speed - (4 + SpeedCar2[i]);
                    if (Cary[i] > 800)
                    {
                        FlagCar[i] = 0;
                    }
                }
                if ((Speed <= 8) && (Speed > 0))
                {
                    score += 5;
                }
                else if (Speed <= 12)
                {
                    score += 15;
                }
                else
                {
                    score += 25;
                }
            }
        }
        private void timer6_Tick(object sender, EventArgs e) // спидометр
        {
            if ((Speed < 8)&&(Speed>0))
            {
                if (flagSpeed2 == 0)
                {
                    SpeedShow += 1;
                }
                else
                {
                    SpeedShow -= 1;
                }
            }
            if ((Speed > 8) && (Speed < 12))
            {
                if (flagSpeed3 == 0)
                {
                    SpeedShow += 1;
                }
                else
                {
                    SpeedShow -= 1;
                }
            }
            if ((Speed > 12) && (Speed < 16))
            {
                if (flagSpeed4 == 0)
                {
                    SpeedShow += 1;
                }
                else
                {
                    SpeedShow -= 1;
                }
            }
        }
        private void PixelCars_KeyPress(object sender, KeyPressEventArgs e) //запись имени
        {
            symbol = e.KeyChar;
            if (symbol == '\u0008' && NamePlayer.Length > 0 )
            {
                StringBuilder newText = new StringBuilder(NamePlayer);
                newText.Remove(NamePlayer.Length - 1, 1);
                NamePlayer = newText.ToString();
            }
            else
            {
                if ((Char.IsLetterOrDigit(symbol) || Char.IsPunctuation(symbol)) && (NamePlayer.Length < 6))
                {
                    NamePlayer += symbol;
                }
            }
        }
        private void LoadRecords() //загрузка рекордов из файла
        {
            int i = 0;
            string path = @"MyRecord.txt";
            using (StreamReader sr = new StreamReader(path))
            {
                while ((sr.Peek() > -1))
                {
                    if (flagWriteName == 0)
                    {
                        Record = (sr.ReadLine());
                        RecordName[i] = Record;
                        flagWriteName = 1;
                    }
                    if (flagWriteName == 1)
                    {
                        Record = (sr.ReadLine());
                        RecordScore[i] = Convert.ToInt32(Record);
                        flagWriteName = 0;
                        i++;
                    }
                }
                CountRecord = i;
            }
            i = 0;
            if (flagGameOver == 2)
            {
                SortingRecords();
                flagGameOver = 0;
            }
        }
        private void SortingRecords() //cjhnbhjdrf рекордов
        {
            int m=0;
            for (int i = 0; i < CountRecord-1; i++)
                for (int j = i + 1; j < CountRecord; j++)
                    if (RecordScore[i] < RecordScore[j])
                    {
                        int t = RecordScore[i];
                        RecordScore[i] = RecordScore[j];
                        RecordScore[j] = t;
                        string t2 = RecordName[i];
                        RecordName[i] = RecordName[j];
                        RecordName[j] = t2;
                    }
            string path = @"MyRecord.txt";
            using (StreamWriter sw = new StreamWriter(path, false))
            {
                while (m < 13)
                {
                    sw.Write(RecordName[m] + "\n" + Convert.ToString(RecordScore[m]) + "\n");
                    m++;
                }
            }
        }
        private void ShowRecords(Graphics g) //загрузка рекордов из файла
        {
            int i = 0;
            if (MainFlag == 3)
            {
                while ((i < 12)&&(i<CountRecord))
                {
                    g.DrawString(Convert.ToString(RecordName[i]) + "\t" + "\t" + Convert.ToString(RecordScore[i] + "\n"), frontstart, new SolidBrush(Color.White), tochkaScore3[i]);
                    i++;
                }
                i = 0;
            }
            if (MainFlag == 1)
            {
                for (i = 0; i < CountRecord; i++)
                {
                    if (RecordScore[i] > max)
                    {
                        max = RecordScore[i];
                    }
                }
                g.DrawString(Convert.ToString(max), sp, new SolidBrush(Color.White), tochkaRecord2);
            }
        }
        private void WriteRecords() //запись рекордов в файл
        {
            string path = @"MyRecord.txt";
            using (StreamWriter sw = new StreamWriter(path, true))
            {
                sw.Write(Player + "\n" + Convert.ToString(score) + "\n");
            }
        }
        private void Perestroenie()
        {
            if (FlagCar[j] == 1)
            {
                if (flagperestroenie == 1)
                {
                    Carx[j] -= 1;
                    {
                        if (Carx[j] == PositionCar[k - 1])
                        {
                            flagperestroenie = 0;
                        }
                    }
                }
                if (flagperestroenie == 2)
                {
                    Carx[j] += 1;
                    {
                        if (Carx[j] == PositionCar[k + 1])
                        {
                            flagperestroenie = 0;
                        }
                    }
                }
            }
        }
        private void timer7_Tick(object sender, EventArgs e) //пересторение машинок
        {
            k = 0;
            per = 0; 
            int i2 = 1;
            flagperestroenie = 0;
            Random rand = new Random(DateTime.Now.Millisecond);
            j = rand.Next(0, 10);
            if (FlagCar[j] == 1)
            {
                if (Cary[j] > 0 && Cary[j] < 400)
                {
                    for (i2 = 0; i2 < 6; i2++)
                    {
                        if (Carx[j] == PositionCar[i2])
                        {
                            for (int i3 = 0; i3 < 10; i3++)
                            {
                                k = i2;
                                if (j <= 4)
                                {
                                    if (i2 != 0)
                                    {
                                        if (!((Cary[j] - 20 <= Cary[i3] + 50) && (Cary[j] - 20 >= Cary[i3] - 20)) || !((Cary[j] + 50 <= Cary[i3] + 50) && (Cary[j] + 50 >= Cary[i3] - 20)))
                                        {
                                            per = Carx[j] - PositionCar[i2 - 1];
                                            flagperestroenie = 1;
                                        }
                                    }
                                }
                                else
                                {
                                    if (i2 != 5)
                                    {
                                        if (!((Cary[j] - 20 <= Cary[i3] + 50) && (Cary[j] - 20 >= Cary[i3] - 20)) || !((Cary[j] + 50 <= Cary[i3] + 50) && (Cary[j] + 50 >= Cary[i3] - 20)))
                                        {
                                            per = PositionCar[i2 + 1] - Carx[j];
                                            flagperestroenie = 2;
                                        }
                          
                }
            }
        }
    }
}
